package com.weixin.paleituo.activity;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.weixin.paleituo.R;
import com.weixin.paleituo.adapter.GoodsHomeGridAdapter;
import com.weixin.paleituo.adapter.GoodsHomePagerAdapter;
import com.weixin.paleituo.base.BasePersonActivity;
import com.weixin.paleituo.bean.ChosenBannerBean;
import com.weixin.paleituo.bean.ChosenCarouselBean;
import com.weixin.paleituo.bean.ChosenGoodsClassListBean;
import com.weixin.paleituo.bean.GoodsMoreBean;
import com.weixin.paleituo.bean.StatusCode;
import com.weixin.paleituo.contans.Contans;
import com.weixin.paleituo.help.Holder;
import com.weixin.paleituo.help.HolderCreator;
import com.weixin.paleituo.http.HttpUtil;
import com.weixin.paleituo.http.ProgressSubscriber;
import com.weixin.paleituo.rxjava.ApiUtils;
import com.weixin.paleituo.util.ToastUtils;
import com.weixin.paleituo.util.UIUtils;
import com.weixin.paleituo.view.LoadingPagerHead;
import com.weixin.paleituo.view.LoopViewPager;
import com.weixin.paleituo.view.MyGridView;
import com.xiaweizi.cornerslibrary.CornersProperty;
import com.xiaweizi.cornerslibrary.RoundCornersTransformation;

import java.util.List;

import butterknife.BindView;
import io.reactivex.Observable;

/**
 * 更多
 */

public class GoodsMoreActivity extends BasePersonActivity {


    @BindView(R.id.viewPager_item_recycleview_activity_goods_more)
    LoopViewPager mViewPagerItemRecycleviewActivityGoodsMore;
    @BindView(R.id.mygrideview_activity_goods_more)
    MyGridView mMygrideviewActivityGoodsMore;
    private LoadingPagerHead mLoadingPagerHead;
    private int mGoodsclassid;
    private List<ChosenGoodsClassListBean> mChosenGoodsClassList;
    private RoundCornersTransformation mRoundCornersTransformation;

    @Override
    protected View getContentView(Context context) {

        mLoadingPagerHead = new LoadingPagerHead(this) {
            @Override
            protected void close() {

            }

            @Override
            protected View createSuccessView() {
                return UIUtils.inflate(GoodsMoreActivity.this, R.layout.activity_goods_more);
            }

            @Override
            protected void reLoading() {

                retry();

            }
        };

      //  setContentView(R.layout.activity_goods_more);
        return mLoadingPagerHead;
    }
    /**
     * 重新访问
     */
    private void retry() {
     //   getMoreShop
        Observable<StatusCode<GoodsMoreBean>> moreShopObservable = ApiUtils.getApi().getMoreShop(Contans.defaultToken, mGoodsclassid);
        HttpUtil.getInstance().toSubscribe(moreShopObservable, new ProgressSubscriber<GoodsMoreBean>(this) {
            @Override
            protected void _onNext(StatusCode<GoodsMoreBean> goodsMoreBeanStatusCode) {
                GoodsMoreBean data = goodsMoreBeanStatusCode.getData();
                ChosenBannerBean chosenBanner = data.getChosenBanner();
                List<ChosenCarouselBean> chosenCarousel = data.getChosenCarousel();
                mChosenGoodsClassList = data.getChosenGoodsClassList();
                if (goodsMoreBeanStatusCode.isFlag()&&chosenBanner!=null&&chosenCarousel!=null &&chosenCarousel.size()>0 && mChosenGoodsClassList !=null&& mChosenGoodsClassList.size()>0){
                    mTvTitle.setText(chosenBanner.getName());
                    mTvTitle.setVisibility(View.VISIBLE);
                    mTvTitle.setTextColor(UIUtils.getColor(R.color.c_ffffffff));
                  mLoadingPagerHead.showPagerView(Contans.STATE_SUCCEED);

                    GoodsHomeGridAdapter goodsHomeGridAdapter=new GoodsHomeGridAdapter(GoodsMoreActivity.this, mChosenGoodsClassList);
                    mMygrideviewActivityGoodsMore.setAdapter(goodsHomeGridAdapter);



                    mViewPagerItemRecycleviewActivityGoodsMore.setPageMargin(UIUtils.dip2px(20));
                    mViewPagerItemRecycleviewActivityGoodsMore.setOffscreenPageLimit(3);
                    GoodsHomePagerAdapter goodsHomePagerAdapter=new GoodsHomePagerAdapter(GoodsMoreActivity.this, chosenCarousel, new HolderCreator<ImageViewHolder>() {
                        @Override
                        public ImageViewHolder createHolder() {
                            return new ImageViewHolder();
                        }
                    });
                    mViewPagerItemRecycleviewActivityGoodsMore.setAdapter(goodsHomePagerAdapter,true);
                    mViewPagerItemRecycleviewActivityGoodsMore.startTurning(3000);
                }else{

                  mLoadingPagerHead.showPagerView(Contans.STATE_EMPTY);
                }
            }
            @Override
            protected void _onError(String message) {

                ToastUtils.makeText(message);
                mLoadingPagerHead.showPagerView(Contans.STATE_ERROR);

            }
        },"",lifecycleSubject,false,true);

    }

    @Override
    protected void initEvent() {
        super.initEvent();
        mMygrideviewActivityGoodsMore.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (mChosenGoodsClassList!=null){

GoodsListActivity.startActivity(GoodsMoreActivity.this,mChosenGoodsClassList.get(position).getChosenclassid(),1);

}
            }
        });
    }

    @Override
    protected void initData() {
        super.initData();
        Intent intent = getIntent();
        mGoodsclassid = intent.getIntExtra(Contans.INTENT_DATA, 0);
        mRlHeadRoot.setBackgroundColor(UIUtils.getColor(R.color.c_29CE7E));
        mIvBack.setImageResource(R.mipmap.fh_w);
        CornersProperty cornersProperty=new CornersProperty();
        cornersProperty.setCornersType(CornersProperty.CornerType.ALL);
        cornersProperty.setCornersRadius(UIUtils.getDimens(R.dimen.dp_6));
        mRoundCornersTransformation = new RoundCornersTransformation(this,cornersProperty);

        retry();
    }

public static void startActivity(Context context,int goodsclassid){
    Intent intent=new Intent(context,GoodsMoreActivity.class);
    intent.putExtra(Contans.INTENT_DATA,goodsclassid);
    context.startActivity(intent);
}
    class ImageViewHolder implements Holder<ChosenCarouselBean> {
        ImageView imageView;
        @Override
        public View createView(Context context) {
            imageView   =new ImageView(context);
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);

            // container.addView(imageView)

            return imageView;
        }

        @Override
        public void UpdateUI(Context context, int position, final ChosenCarouselBean data) {
            // imageView.setImageResource(R.mipmap.originality_ad);

            Glide.with(context).load(data.getImg()).bitmapTransform(mRoundCornersTransformation).into(imageView);
            imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    GoodsDetailsActivity.startActivity(GoodsMoreActivity.this,data.getGoodsid());
                }
            });

        }
    }

}
